﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class staff_lessons_list : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["StaffID"] != null)
            {
                string strStaffID = Session["StaffID"].ToString();
                string strStaffName = Session["StaffName"].ToString();
                string strStaffNodesID = Session["StaffNodesID"].ToString();
                string strStaffNodesName = Session["StaffNodesName"].ToString();
                this.lbl_StafInfo.Text = "(" + strStaffName + "[" + strStaffID + "][" + strStaffNodesName + "])";
                this.lbtn_Exit.Visible = true;
                this.StaffLogin.Visible = false;
            }
            else
            {
                this.lbtn_Exit.Visible = false;
                this.StaffLogin.Visible = true;
            }
            if (!IsPostBack)
            {
            }
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/staff_lessons_finish_list.aspx";
            Response.Redirect(url);
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/cpntraining_lessons_list.aspx";
            Response.Redirect(url);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.LoadRep();
        }

        //绑定Rep控件
        public void LoadRep()
        {
            if (Session["StaffID"] == null) return;
            string strwhere = this.GetStrWhere();
            this.AspNetPager1.RecordCount = new Catering_Rules.NodesLessons().ViewGetRecordCount(strwhere);
            DataTable dt_PurchaseInfo = new Catering_Rules.NodesLessons().ViewGetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, strwhere).Tables[0];//分页处理
            this.Repeater1.DataSource = dt_PurchaseInfo;
            this.Repeater1.DataBind();
        }

        //获取查询条件
        public string GetStrWhere()
        {
            StringBuilder sb = new StringBuilder("");

            if (!string.IsNullOrEmpty(this.txt_BookName.Text.Trim()))
            {
                if (this.Drop_SearchType.SelectedValue == "LessonName")
                {
                    sb.Append(" and LessonName like '%" + this.txt_BookName.Text.Trim() + "%' ");
                }
                else if (this.Drop_SearchType.SelectedValue == "LessonType")
                {

                }
            }
            if (Session["StaffNodesID"] != null)
            {
                sb.Append(" and NodeID= " + Session["StaffNodesID"].ToString() + " ");
            }
            return sb.ToString();
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep();
        }

        protected void btn_ReturnIndex_Click(object sender, EventArgs e)
        {

        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int LessonID = int.Parse(e.CommandArgument.ToString());
            if (LessonID <= 0) return;
            if (e.CommandName == "study")
            {
                //staff_lessons_learn.aspx
                string url = "~/UserTraining/SlrCompanyTrainning/staff_lessons_learn.aspx?LessionID=" + LessonID;
                Response.Redirect(url);
            }
            else if (e.CommandName == "down")
            {

            }
            else if (e.CommandName == "test")
            {
                string url = "~/UserTraining/SlrCompanyTrainning/staff_lessons_test.aspx?LessionID=" + LessonID;
                Response.Redirect(url);
            }
        }

        /// <summary>
        /// 学员登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_UserLog_Click(object sender, EventArgs e)
        {
            string strUserName = this.txt_UserID.Text.Trim();
            string strUserPass = this.txt_UserPass.Text.Trim();
            if (!string.IsNullOrEmpty(strUserName) && !string.IsNullOrEmpty(strUserPass))
            {
                Catering_Model.Staff _StaffModel = new Catering_Model.Staff();

                _StaffModel = new Catering_Rules.Staff().GetModelByUserName(strUserName);
                if (_StaffModel != null && _StaffModel.UserPass == strUserPass)
                {
                    Catering_Model.StaffNodes _StaffNodesModel = new Catering_Model.StaffNodes();
                    Catering_Model.BranchPost _NodesModel = new Catering_Model.BranchPost();
                    _StaffNodesModel = new Catering_Rules.StaffNodes().GetModel(_StaffModel.UserID);
                    if (_StaffNodesModel == null)
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('用户ID或密码错误！')", true);
                        return;
                    }
                    _NodesModel = new Catering_Rules.BranchPost().GetModel(_StaffNodesModel.NodeID);
                    if (_NodesModel == null)
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('用户ID暂无相关岗位权限！')", true);
                        return;
                    }
                    Session["StaffID"] = _StaffModel.UserID;
                    Session["StaffName"] = _StaffModel.UserName;
                    Session["StaffNodesID"] = _StaffNodesModel.NodeID.ToString();
                    Session["StaffNodesName"] = _NodesModel.Name;
                    this.lbl_StafInfo.Text = "(" + _StaffModel.UserName + "[" + _StaffModel.UserID + "][" + _NodesModel.Name + "])";
                    this.lbtn_Exit.Visible = true;
                    this.StaffLogin.Visible = false;
                }
            }
        }

        protected void lbtn_Exit_Click(object sender, EventArgs e)
        {
            Session["StaffID"] = null;
            Session["StaffName"] = null;
            Session["StaffNodesID"] = null;
            Session["StaffNodesName"] = null;
            this.lbl_StafInfo.Text = "";
            this.lbtn_Exit.Visible = false;
            this.StaffLogin.Visible = true;
        }

        public string GetTestPoints(string strLession, string strNodeID)
        {
            string strUserID = Session["StaffID"] == null ? "" : Session["StaffID"].ToString();
            Catering_Model.StaffLessonsLog _StaffLessonsLogModel = new Catering_Model.StaffLessonsLog();
            _StaffLessonsLogModel = new Catering_Rules.StaffLessonsLog().GetModel(int.Parse(strLession), int.Parse(strNodeID), int.Parse(strUserID));
            if (_StaffLessonsLogModel != null) return _StaffLessonsLogModel.TestPoints.ToString();
            return "";
        }

        public string IsGood(string strLession, string strNodeID)
        {
            string strPoints = GetTestPoints(strLession, strNodeID);
            if (string.IsNullOrEmpty(strPoints))
            {
                return "";
            }
            else if (Convert.ToDecimal(strPoints) < 60)
            {
                return "否";
            }
            return "是";
        }

        public string GetTestTime(string strLession, string strNodeID)
        {
            string strUserID = Session["StaffID"] == null ? "" : Session["StaffID"].ToString();
            Catering_Model.StaffLessonsLog _StaffLessonsLogModel = new Catering_Model.StaffLessonsLog();
            _StaffLessonsLogModel = new Catering_Rules.StaffLessonsLog().GetModel(int.Parse(strLession), int.Parse(strNodeID), int.Parse(strUserID));
            if (_StaffLessonsLogModel != null && Convert.ToDateTime(_StaffLessonsLogModel.TestDate.ToString()).Year > 2000) return _StaffLessonsLogModel.TestDate.ToString();
            return "";
        }

        public string GetLessionTypeName(string strLessionTypeID)
        {
            Catering_Model.LessonType _LessonTypeModel = new Catering_Rules.LessonType().GetModel(int.Parse(strLessionTypeID));
            if (_LessonTypeModel == null) return "";
            return _LessonTypeModel.LessonTypeName;
        }

        public string GetNodeName(string strNodeID)
        {
            Catering_Model.BranchPost _LessonTypeModel = new Catering_Rules.BranchPost().GetModel(int.Parse(strNodeID));
            if (_LessonTypeModel == null) return "";
            return _LessonTypeModel.Name;
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                LinkButton lbtn1 = e.Item.FindControl("LinkButton1") as LinkButton;
                LinkButton lbtn3 = e.Item.FindControl("LinkButton3") as LinkButton;
                int LessonID = int.Parse(lbtn1.CommandArgument.ToString());
                if (LessonID <= 0)
                {
                    lbtn1.Visible = false;
                    lbtn3.Visible = false;
                }
                else
                {
                    Catering_Model.StaffLessonsLog _ModelStaffLessonsLog = new Catering_Model.StaffLessonsLog();
                    _ModelStaffLessonsLog = new Catering_Rules.StaffLessonsLog().GetModel(LessonID, int.Parse(Session["StaffNodesID"].ToString()), int.Parse(Session["StaffID"].ToString()));
                    if (_ModelStaffLessonsLog == null || _ModelStaffLessonsLog.UserEndDate.Year < 2000)
                    {
                        lbtn3.Visible = false;
                    }
                    else if (_ModelStaffLessonsLog != null && _ModelStaffLessonsLog.UserEndDate.Year > 2000)
                    {
                        lbtn1.Visible = false;
                    }
                }
            }
        }
    }
}